package edu.fub.pub2search.app;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Paint.Style;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

import edu.fub.pub2search.app.util.GeoCalculator;


/**
 * Class to manage the overlay (marker) of the person and searching radius. 
 * 
 * @author Andrej
 */
public class PersonOverlay extends Overlay {
	
	/** Image for representing a person */
	private Bitmap personIcon;
	
	/** Coordinate for a person location */
	private GeoPoint personGeoPoint;
	
	/** The searching radius size, in meters */
	private int radius;
	
	/** The MapViewer object for managing the user screen map */
	MapViewer mapViewer;
	
	/**
	 * Default constructor.
	 * 
	 * @param mapViewer The MapViewer object.
	 */
	public PersonOverlay(GeoPoint personGeoPoint, int radius, MapViewer mapViewer) {
		
		this.personGeoPoint = personGeoPoint;
		this.radius = radius;
		this.mapViewer = mapViewer;
		
		// setup the person icon.
		personIcon = BitmapFactory.decodeResource(mapViewer.getResources(), R.drawable.ic_map_user);
	}
	
	@Override
	public void draw(Canvas canvas, MapView	mapView, boolean shadow) {
   
   	 	drawCircleRadius(canvas, mapView);
   	 	drawPersonLocation(canvas, mapView);
    }
	
// ---------------- PRIVATE METHOD ------------------
	

	/*
	 * Draw the person icon to the screen map.
	 */
	private void drawPersonLocation(Canvas canvas, MapView mapView) {
		
		// map to a screen point
		Point point = new Point();
		mapView.getProjection().toPixels(personGeoPoint, point);	
		
		// draw the person icon
    	canvas.drawBitmap(personIcon, 							// the icon image
    					  point.x - personIcon.getWidth()/2, 	// position on center of the icon
    					  point.y - personIcon.getHeight()/2, 
    					  null);								// no additional painter
    	
	}
	
	/*
	 *  Draw the illustration of searching area (in circle).
	 */
	private void drawCircleRadius(Canvas canvas, MapView mapView) {
		
		Paint innerPaint = new Paint();
		Paint borderPaint = new Paint();
		
		// make transparent
		innerPaint.setARGB(20, 119, 0, 255);
		innerPaint.setAntiAlias(true);
      
		borderPaint.setARGB(100, 119, 0, 255);
		borderPaint.setAntiAlias(true);
		borderPaint.setStyle(Style.STROKE);
		borderPaint.setStrokeWidth(2);
		
		// projection serves as a translator between the coordinate system of x/y on-screen pixel coordinates 
		// and that of latitude/longitude points on the surface of the earth. 
		Projection projection = mapView.getProjection();
		
		// map to a screen point.
		Point point = new Point();
		projection.toPixels(personGeoPoint, point);
		
		
		
		// Calculate pixels from metres with respect to longitude and latitude of GeoPoint
		int markerRadius = GeoCalculator.metresToPixelsWithRespectToGeoPoint(projection, (double) radius/1000, personGeoPoint);
		
		
		RectF oval = new RectF(point.x-markerRadius,
                             point.y-markerRadius,
                             point.x+markerRadius,
                             point.y+markerRadius);
		
		// draw the circle by putting the colors.
		canvas.drawOval(oval,innerPaint);
		canvas.drawOval(oval,borderPaint);
	}
	
}
